#include "lolevel.h"
#include "platform.h"
#include "core.h"

static long *nrflag = (long*)0x7FD0; /* S90 above "aShuttersoundta" usage @FF966918 @101a */

#include "../../../generic/capt_seq.c"

void __attribute__((naked,noinline)) capt_seq_task() {
 asm volatile (
        "STMFD   SP!, {R3-R9,LR}\n"
        "LDR     R6, =0x2C58\n"
        "LDR     R4, =0x39F60\n"
        "MOV     R9, #1\n"
        "MOV     R7, #0\n"
"loc_FF87A21C:\n"
        "LDR     R0, [R6,#4]\n"
        "MOV     R2, #0\n"
        "MOV     R1, SP\n"
        "BL      sub_FF8382FC\n"
        "TST     R0, #1\n"
        "BEQ     loc_FF87A248\n"
        "LDR     R1, =0x5B3\n"
        "LDR     R0, =0xFF879A2C\n"
        "BL      sub_FF81E88C\n"
        "BL      sub_FF81E844\n"
        "LDMFD   SP!, {R3-R9,PC}\n"
"loc_FF87A248:\n"
        "LDR     R0, [SP]\n"
        "LDR     R1, [R0]\n"
        "CMP     R1, #0x21\n"
        "ADDLS   PC, PC, R1,LSL#2\n"
        "B       loc_FF87A50C\n"
"loc_FF87A25C:\n"
        "B       loc_FF87A2E4\n"
"loc_FF87A260:\n"
        "B       loc_FF87A350\n"
"loc_FF87A264:\n"
        "B       loc_FF87A38C\n"
"loc_FF87A268:\n"
        "B       loc_FF87A3A0\n"
"loc_FF87A26C:\n"
        "B       loc_FF87A398\n"
"loc_FF87A270:\n"
        "B       loc_FF87A3A8\n"
"loc_FF87A274:\n"
        "B       loc_FF87A3B0\n"
"loc_FF87A278:\n"
        "B       loc_FF87A3B8\n"
"loc_FF87A27C:\n"
        "B       loc_FF87A410\n"
"loc_FF87A280:\n"
        "B       loc_FF87A438\n"
"loc_FF87A284:\n"
        "B       loc_FF87A41C\n"
"loc_FF87A288:\n"
        "B       loc_FF87A428\n"
"loc_FF87A28C:\n"
        "B       loc_FF87A430\n"
"loc_FF87A290:\n"
        "B       loc_FF87A440\n"
"loc_FF87A294:\n"
        "B       loc_FF87A448\n"
"loc_FF87A298:\n"
        "B       loc_FF87A450\n"
"loc_FF87A29C:\n"
        "B       loc_FF87A458\n"
"loc_FF87A2A0:\n"
        "B       loc_FF87A460\n"
"loc_FF87A2A4:\n"
        "B       loc_FF87A468\n"
"loc_FF87A2A8:\n"
        "B       loc_FF87A470\n"
"loc_FF87A2AC:\n"
        "B       loc_FF87A478\n"
"loc_FF87A2B0:\n"
        "B       loc_FF87A480\n"
"loc_FF87A2B4:\n"
        "B       loc_FF87A488\n"
"loc_FF87A2B8:\n"
        "B       loc_FF87A494\n"
"loc_FF87A2BC:\n"
        "B       loc_FF87A49C\n"
"loc_FF87A2C0:\n"
        "B       loc_FF87A4A8\n"
"loc_FF87A2C4:\n"
        "B       loc_FF87A4B0\n"
"loc_FF87A2C8:\n"
        "B       loc_FF87A4B8\n"
"loc_FF87A2CC:\n"
        "B       loc_FF87A4C0\n"
"loc_FF87A2D0:\n"
        "B       loc_FF87A4C8\n"
"loc_FF87A2D4:\n"
        "B       loc_FF87A4D0\n"
"loc_FF87A2D8:\n"
        "B       loc_FF87A4D8\n"
"loc_FF87A2DC:\n"
        "B       loc_FF87A4E4\n"
"loc_FF87A2E0:\n"
        "B       loc_FF87A518\n"
"loc_FF87A2E4:\n"
        "BL      sub_FF87AB20\n"
		"BL      shooting_expo_param_override\n"  // + 
        "BL      sub_FF877B70\n"

// copied over from SX10 don't know if we need it yet
//  this code added to avoid some incorrect behavior if overrides are used.
//  but it can cause some unexpected side effects. In this case, remove this code!
                 "MOV     R0, #0\n"
                 "STR     R0, [R4,#0x24]\n"  // fixes overrides  behavior at short shutter press
 //  end of my code

        "LDR     R0, [R4,#0x24]\n"
        "CMP     R0, #0\n"
        "BEQ     loc_FF87A518\n"
        "BL      sub_FF879700\n"
        "MOV     R5, R0\n"
        "LDR     R0, [R4,#0x24]\n"
        "CMP     R0, #0\n"
        "BEQ     loc_FF87A32C\n"
        "MOV     R0, #0xC\n"
        "BL      sub_FF87F92C\n"
        "TST     R0, #1\n"
        "STRNE   R9, [R6,#0x10]\n"
        "LDRNE   R0, [R5,#8]\n"
        "ORRNE   R0, R0, #0x40000000\n"
        "STRNE   R0, [R5,#8]\n"
        "BNE     loc_FF87A518\n"
"loc_FF87A32C:\n"
        "MOV     R0, R5\n"
        "BL      sub_FF966014\n"
        "MOV     R0, R5\n"
        "BL      sub_FF879AE0\n"
        "MOV     R0, R5\n"
        //"BL      sub_FF9668B0\n"
		"BL      sub_FF9668B0_my\n" // -------------->
		"BL      capt_seq_hook_raw_here\n"      // +
        "TST     R0, #1\n"
        "STRNE   R9, [R6,#0x10]\n"
        "B       loc_FF87A518\n"
"loc_FF87A350:\n"
        "LDR     R0, [R4,#0x24]\n"
        "CMP     R0, #0\n"
        "BNE     loc_FF87A37C\n"
        "MOV     R0, #0xC\n"
        "BL      sub_FF87F92C\n"
        "TST     R0, #1\n"
        "LDRNE   R0, [SP]\n"
        "MOVNE   R1, #1\n"
        "LDRNE   R2, [R0,#0xC]\n"
        "MOVNE   R0, #1\n"
        "BNE     loc_FF87A408\n"
"loc_FF87A37C:\n"
        "LDR     R0, [SP]\n"
        //"BL      sub_FF879B58\n" // - capt_seq_hook ist called in this subroutine
		"BL      sub_FF879B58_my\n" // ------------------->
"loc_FF87A384:\n"
        "STR     R7, [R4,#0x24]\n"
        "B       loc_FF87A518\n"
"loc_FF87A38C:\n"
        "MOV     R0, #1\n"
        "BL      sub_FF87AD74\n"
        "B       loc_FF87A518\n"
"loc_FF87A398:\n"
        "BL      sub_FF87A7D0\n"
        "B       loc_FF87A384\n"
"loc_FF87A3A0:\n"
        "BL      sub_FF87AB00\n"
        "B       loc_FF87A384\n"
"loc_FF87A3A8:\n"
        "BL      sub_FF87AB08\n"
        "B       loc_FF87A518\n"
"loc_FF87A3B0:\n"
        "BL      sub_FF87AC94\n"
        "B       loc_FF87A414\n"
"loc_FF87A3B8:\n"
        "LDR     R5, [R0,#0xC]\n"
        "BL      sub_FF87AB10\n"
        "MOV     R0, R5\n"
        "BL      sub_FF9654A0\n"
        "TST     R0, #1\n"
        "MOV     R8, R0\n"
        "BNE     loc_FF87A3F8\n"
        "BL      sub_FF88CBF4\n"
        "STR     R0, [R5,#0x18]\n"
        "MOV     R0, R5\n"
        "BL      sub_FF9667C8\n"
        "MOV     R0, R5\n"
        "BL      sub_FF966BC8\n"
        "MOV     R8, R0\n"
        "LDR     R0, [R5,#0x18]\n"
        "BL      sub_FF88CE08\n"
"loc_FF87A3F8:\n"
        "BL      sub_FF87AB00\n"
        "MOV     R2, R5\n"
        "MOV     R1, #9\n"
        "MOV     R0, R8\n"
"loc_FF87A408:\n"
        "BL      sub_FF8780D8\n"
        "B       loc_FF87A518\n"
"loc_FF87A410:\n"
        "BL      sub_FF87ACF4\n"
"loc_FF87A414:\n"
        "BL      sub_FF877B70\n"
        "B       loc_FF87A518\n"
"loc_FF87A41C:\n"
        "LDR     R0, [R4,#0x54]\n"
        "BL      sub_FF87B5D4\n"
        "B       loc_FF87A518\n"
"loc_FF87A428:\n"
        "BL      sub_FF87B888\n"
        "B       loc_FF87A518\n"
"loc_FF87A430:\n"
        "BL      sub_FF87B91C\n"
        "B       loc_FF87A518\n"
"loc_FF87A438:\n"
        "BL      sub_FF87AB00\n"
        "B       loc_FF87A518\n"
"loc_FF87A440:\n"
        "BL      sub_FF9656BC\n"
        "B       loc_FF87A518\n"
"loc_FF87A448:\n"
        "BL      sub_FF9658D0\n"
        "B       loc_FF87A518\n"
"loc_FF87A450:\n"
        "BL      sub_FF965958\n"
        "B       loc_FF87A518\n"
"loc_FF87A458:\n"
        "BL      sub_FF965A48\n"
        "B       loc_FF87A518\n"
"loc_FF87A460:\n"
        "BL      sub_FF965B3C\n"
        "B       loc_FF87A518\n"
"loc_FF87A468:\n"
        "MOV     R0, #0\n"
        "B       loc_FF87A48C\n"
"loc_FF87A470:\n"
        "BL      sub_FF965D58\n"
        "B       loc_FF87A518\n"
"loc_FF87A478:\n"
        "BL      sub_FF965DE8\n"
        "B       loc_FF87A518\n"
"loc_FF87A480:\n"
        "BL      sub_FF965EA8\n"
        "B       loc_FF87A518\n"
"loc_FF87A488:\n"
        "MOV     R0, #1\n"
"loc_FF87A48C:\n"
        "BL      sub_FF965C08\n"
        "B       loc_FF87A518\n"
"loc_FF87A494:\n"
        "BL      sub_FF87AF5C\n"
        "B       loc_FF87A518\n"
"loc_FF87A49C:\n"
        "BL      sub_FF87AFFC\n"
        "BL      sub_FF87A640\n"
        "B       loc_FF87A518\n"
"loc_FF87A4A8:\n"
        "BL      sub_FF87B370\n"
        "B       loc_FF87A518\n"
"loc_FF87A4B0:\n"
        "BL      sub_FF87B48C\n"
        "B       loc_FF87A518\n"
"loc_FF87A4B8:\n"
        "BL      sub_FF8799B8\n"
        "B       loc_FF87A518\n"
"loc_FF87A4C0:\n"
        "BL      sub_FF835FDC\n"
        "B       loc_FF87A518\n"
"loc_FF87A4C8:\n"
        "BL      sub_FF87DCDC\n"
        "B       loc_FF87A518\n"
"loc_FF87A4D0:\n"
        "BL      sub_FF87DD44\n"
        "B       loc_FF87A518\n"
"loc_FF87A4D8:\n"
        "BL      sub_FF87DDA0\n"
        "BL      sub_FF87DD60\n"
        "B       loc_FF87A518\n"
"loc_FF87A4E4:\n"
        "MOV     R0, #1\n"
        "BL      sub_FF967430\n"
        "MOV     R0, #1\n"
        "BL      sub_FF967540\n"
        "LDRH    R0, [R4,#0xA0]\n"
        "CMP     R0, #4\n"
        "BNE     loc_FF87A518\n"
        "BL      sub_FF87DD44\n"
        "BL      sub_FF87E18C\n"
        "B       loc_FF87A518\n"
"loc_FF87A50C:\n"
        "LDR     R1, =0x708\n"
        "LDR     R0, =0xFF879A2C\n"
        "BL      sub_FF81E88C\n"
"loc_FF87A518:\n"
        "LDR     R0, [SP]\n"
        "LDR     R1, [R0,#4]\n"
        "LDR     R0, [R6]\n"
        "BL      sub_FF8848E0\n"
        "LDR     R5, [SP]\n"
        "LDR     R0, [R5,#8]\n"
        "CMP     R0, #0\n"
        "LDREQ   R1, =0x131\n"
        "LDREQ   R0, =0xFF879A2C\n"
        "BLEQ    sub_FF81E88C\n"
        "STR     R7, [R5,#8]\n"
        "B       loc_FF87A21C\n"
 );
}


void __attribute__((naked,noinline)) sub_FF879B58_my() {
 asm volatile (
"STMFD   SP!, {R3-R9,LR}\n"
        "LDR     R4, [R0,#0xC]\n"
        "LDR     R5, =0x39F60\n"
        "LDR     R0, [R4,#8]\n"
        "LDR     R6, =0x420A\n"
        "ORR     R0, R0, #1\n"
        "STR     R0, [R4,#8]\n"
        "LDRH    R0, [R5]\n"
        "LDR     R8, =0x2C58\n"
        "MOV     R7, #0\n"
        "CMP     R0, R6\n"
        "BEQ     loc_FF879BFC\n"
        "LDRH    R0, [R5,#0x9E]\n"
        "CMP     R0, #3\n"
        "BEQ     loc_FF879C5C\n"
        "LDR     R0, [R4,#0xC]\n"
        "CMP     R0, #1\n"
        "BLS     loc_FF879C08\n"
        "LDRH    R0, [R5,#0x9C]\n"
        "CMP     R0, #0\n"
        "BNE     loc_FF879C5C\n"
        "LDRH    R0, [R5,#0x98]\n"
        "CMP     R0, #2\n"
        "BNE     loc_FF879C14\n"
        "BL      sub_FF87B0AC\n"
        "LDRH    R0, [R5]\n"
        "CMP     R0, R6\n"
        "BEQ     loc_FF879BFC\n"
        "LDRH    R0, [R5,#0x9E]\n"
        "CMP     R0, #3\n"
        "BEQ     loc_FF879C5C\n"
        "LDR     R0, [R4,#0xC]\n"
        "CMP     R0, #1\n"
        "BLS     loc_FF879C08\n"
        "LDRH    R0, [R5,#0x9C]\n"
        "CMP     R0, #0\n"
        "BNE     loc_FF879C5C\n"
        "LDRH    R0, [R5,#0x98]\n"
        "CMP     R0, #2\n"
        "BEQ     loc_FF879C40\n"
        "B       loc_FF879C14\n"
"loc_FF879BFC:\n"
        "LDRH    R0, [R5,#0x9E]\n"
        "CMP     R0, #3\n"
        "BEQ     loc_FF879C5C\n"
"loc_FF879C08:\n"
        "LDRH    R0, [R5,#0x9C]\n"
        "CMP     R0, #0\n"
        "BNE     loc_FF879C5C\n"
"loc_FF879C14:\n"
        "LDRH    R0, [R5,#0x98]\n"
        "CMP     R0, #1\n"
        "BNE     loc_FF879C5C\n"
        "LDRH    R0, [R5]\n"
        "CMP     R0, R6\n"
        "LDRNE   R0, [R4,#0xC]\n"
        "CMPNE   R0, #1\n"
        "BLS     loc_FF879C5C\n"
        "LDR     R0, [R4,#0x10]\n"
        "CMP     R0, #1\n"
        "BNE     loc_FF879C5C\n"
"loc_FF879C40:\n"
        "MOV     R3, #0x268\n"
        "STR     R3, [SP]\n"
        "LDR     R0, [R8]\n"
        "LDR     R2, =0xEA60\n"
        //"ADR     R3, 0xFF879A2C\n"
		"LDR     R3, =0xFF879A2C\n"
        "MOV     R1, #0x40000000\n"
        "BL      sub_FF87FC8C\n"
"loc_FF879C5C:\n"
        "BL      sub_FF8799B8\n"
        "LDR     R0, [R5,#0x24]\n"
        "CMP     R0, #0\n"
        "MOVEQ   R0, #2\n"
        "BLEQ    sub_FF8762DC\n"
        "BL      sub_FF87AB10\n"
        "LDR     R0, [R5,#0x24]\n"
        "CMP     R0, #0\n"
        "BNE     loc_FF879D14\n"
        "MOV     R0, #0\n"
        "BL      sub_FF967430\n"
        "MOV     R0, #0\n"
        "BL      sub_FF967540\n"
        "MOV     R0, R4\n"
        "BL      sub_FF966014\n"
        "MOV     R0, R4\n"
        "BL      sub_FF87AED8\n"
        "MOV     R0, R4\n"
        "BL      sub_FF965090\n"
        "CMP     R0, #0\n"
        "BEQ     loc_FF879CDC\n"
        "BL      sub_FF967470\n"
        "BL      sub_FF967584\n"
        "BL      sub_FF9675D4\n"
        "MOV     R0, R4\n"
        "BL      sub_FF9651D4\n"
        "TST     R0, #1\n"
        "MOVNE   R2, R4\n"
        "LDMNEFD SP!, {R3-R9,LR}\n"
        "MOVNE   R1, #1\n"
        "BNE     sub_FF8780D8\n"
        "B       loc_FF879CF0\n"
"loc_FF879CDC:\n"
        "MOV     R0, R4\n"
        "BL      sub_FF965140\n"
        "BL      sub_FF967470\n"
        "BL      sub_FF967584\n"
        "BL      sub_FF9675D4\n"
"loc_FF879CF0:\n"
        "MOV     R0, R4\n"
        "BL      sub_FF879AE0\n"
        "MOV     R0, R4\n"
        "BL      sub_FF9667C8\n"
        "BL      sub_FF967248\n"
        "MOV     R0, R4\n"
        //"BL      sub_FF9668B0\n"
		"BL      sub_FF9668B0_my\n" // ------------->
        "MOV     R7, R0\n"
		"BL      capt_seq_hook_raw_here\n"      // +
        "B       loc_FF879D20\n"
"loc_FF879D14:\n"
        "LDR     R0, [R8,#0x10]\n"
        "CMP     R0, #0\n"
        "MOVNE   R7, #0x1D\n"
"loc_FF879D20:\n"
        "BL      sub_FF87DD44\n"
        "BL      sub_FF87DD8C\n"
        "BL      sub_FF87DDCC\n"
        "MOV     R2, R4\n"
        "MOV     R1, #1\n"
        "MOV     R0, R7\n"
        "BL      sub_FF8780D8\n"
        "BL      sub_FF966B6C\n"
        "CMP     R0, #0\n"
        "LDRNE   R0, [R4,#8]\n"
        "ORRNE   R0, R0, #0x2000\n"
        "STRNE   R0, [R4,#8]\n"
        "LDRH    R0, [R5,#0x9E]\n"
        "CMP     R0, #3\n"
        "BEQ     locret_FF879D78\n"
        "LDRH    R0, [R5,#0x9C]\n"
        "CMP     R0, #0\n"
        "LDREQH  R0, [R5,#0x98]\n"
        "CMPEQ   R0, #2\n"
        "MOVEQ   R0, R4\n"
        "LDMEQFD SP!, {R3-R9,LR}\n"
        "BEQ     sub_FF87B100\n"
"locret_FF879D78:\n"
        "LDMFD   SP!, {R3-R9,PC}\n"
 );
}

void __attribute__((naked,noinline)) sub_FF9668B0_my() {
 asm volatile (
        "STMFD   SP!, {R0-R8,LR}\n"
        "MOV     R4, R0\n"
        "BL      sub_FF96772C\n"
        "MVN     R1, #0\n"
        "BL      sub_FF884914\n"
        "LDR     R5, =0x7FD0\n"
        "LDR     R0, [R5,#0xC]\n"
        "CMP     R0, #0\n"
        "BNE     loc_FF966900\n"
        "MOV     R1, #1\n"
        "MOV     R0, #0\n"
        "BL      sub_FF838B44\n"
        "STR     R0, [R5,#0xC]\n"
        "MOV     R3, #0\n"
        "STR     R3, [SP]\n"
        "LDR     R3, =0xFF96623C\n"
        //"ADR     R0, 0xFF966B30\n"
		"LDR     R0, =0xFF966B30\n"
        "MOV     R2, #0x400\n"
        "MOV     R1, #0x17\n"
        "BL      sub_FF838B10\n"
"loc_FF966900:\n"
        "MOV     R2, #4\n"
        "ADD     R1, SP, #8\n"
        "MOV     R0, #0x8A\n"
        "BL      sub_FF88CA64\n"
        "TST     R0, #1\n"
        "LDRNE   R1, =0x3C5\n"
        "LDRNE   R0, =0xFF9664D4\n"
        "BLNE    sub_FF81E88C\n"
        "LDR     R6, =0x3A02C\n"
        "LDR     R8, =0x39F60\n"
        "LDR     R3, [R6]\n"
        "LDRSH   R2, [R6,#0xC]\n"
        "LDRSH   R1, [R6,#0xE]\n"
        "LDR     R0, [R8,#0x94]\n"
        "BL      sub_FF92E118\n"
        "BL      sub_FF86157C\n"
        "LDR     R3, =0x7FD8\n"
        "STRH    R0, [R4,#0xA4]\n"
        "SUB     R2, R3, #4\n"
        "STRD    R2, [SP]\n"
        "MOV     R1, R0\n"
        "LDRH    R0, [R8,#0x5C]\n"
        "LDRSH   R2, [R6,#0xC]\n"
        "SUB     R3, R3, #8\n"
        "BL      sub_FF96944C\n"
        "BL      wait_until_remote_button_is_released\n"  // +
        "BL      capt_seq_hook_set_nr\n"                  // +   
        "B       sub_FF966964\n" // +  Jump back to original FW code
 );
}

void __attribute__((naked,noinline)) exp_drv_task(){
 asm volatile(
        "STMFD   SP!, {R4-R8,LR}\n"
        "SUB     SP, SP, #0x20\n"
        "LDR     R8, =0xBB8\n"
        "LDR     R7, =0x4440\n"
        "LDR     R5, =0x4372C\n"
        "MOV     R0, #0\n"
        "ADD     R6, SP, #0x10\n"
        "STR     R0, [SP,#0xC]\n"
"loc_FF8C0E74:\n"
        "LDR     R0, [R7,#0x20]\n"
        "MOV     R2, #0\n"
        "ADD     R1, SP, #0x1C\n"
        "BL      sub_FF8382FC\n"
        "LDR     R0, [SP,#0xC]\n"
        "CMP     R0, #1\n"
        "BNE     loc_FF8C0EC0\n"
        "LDR     R0, [SP,#0x1C]\n"
        "LDR     R0, [R0]\n"
        "CMP     R0, #0x13\n"
        "CMPNE   R0, #0x14\n"
        "CMPNE   R0, #0x15\n"
        "CMPNE   R0, #0x16\n"
        "BEQ     loc_FF8C1024\n"
        "CMP     R0, #0x28\n"
        "BEQ     loc_FF8C0FAC\n"
        "ADD     R1, SP, #0xC\n"
        "MOV     R0, #0\n"
        "BL      sub_FF8C0E04\n"
"loc_FF8C0EC0:\n"
        "LDR     R0, [SP,#0x1C]\n"
        "LDR     R1, [R0]\n"
        "CMP     R1, #0x2E\n"
        "BNE     loc_FF8C0EF0\n"
        "LDR     R0, [SP,#0x1C]\n"
        "BL      sub_FF8C215C\n"
        "LDR     R0, [R7,#0x1C]\n"
        "MOV     R1, #1\n"
        "BL      sub_FF8848E0\n"
        "BL      sub_FF81E844\n"
        "ADD     SP, SP, #0x20\n"
        "LDMFD   SP!, {R4-R8,PC}\n"
"loc_FF8C0EF0:\n"
        "CMP     R1, #0x2D\n"
        "BNE     loc_FF8C0F0C\n"
        "LDR     R2, [R0,#0x8C]!\n"
        "LDR     R1, [R0,#4]\n"
        "MOV     R0, R1\n"
        "BLX     R2\n"
        "B       loc_FF8C144C\n"
"loc_FF8C0F0C:\n"
        "CMP     R1, #0x26\n"
        "BNE     loc_FF8C0F5C\n"
        "LDR     R0, [R7,#0x1C]\n"
        "MOV     R1, #0x80\n"
        "BL      sub_FF884914\n"
        "LDR     R0, =0xFF8BD59C\n"
        "MOV     R1, #0x80\n"
        "BL      sub_FF959524\n"
        "LDR     R0, [R7,#0x1C]\n"
        "MOV     R2, R8\n"
        "MOV     R1, #0x80\n"
        "BL      sub_FF884820\n"
        "TST     R0, #1\n"
        "LDRNE   R1, =0xE5F\n"
        "BNE     loc_FF8C1018\n"
"loc_FF8C0F48:\n"
        "LDR     R1, [SP,#0x1C]\n"
        "LDR     R0, [R1,#0x90]\n"
        "LDR     R1, [R1,#0x8C]\n"
        "BLX     R1\n"
        "B       loc_FF8C144C\n"
"loc_FF8C0F5C:\n"
        "CMP     R1, #0x27\n"
        "BNE     loc_FF8C0FA4\n"
        "ADD     R1, SP, #0xC\n"
        "BL      sub_FF8C0E04\n"
        "LDR     R0, [R7,#0x1C]\n"
        "MOV     R1, #0x100\n"
        "BL      sub_FF884914\n"
        "LDR     R0, =0xFF8BD5AC\n"
        "MOV     R1, #0x100\n"
        "BL      sub_FF959EB4\n"
        "LDR     R0, [R7,#0x1C]\n"
        "MOV     R2, R8\n"
        "MOV     R1, #0x100\n"
        "BL      sub_FF884820\n"
        "TST     R0, #1\n"
        "BEQ     loc_FF8C0F48\n"
        "LDR     R1, =0xE69\n"
        "B       loc_FF8C1018\n"
"loc_FF8C0FA4:\n"
        "CMP     R1, #0x28\n"
        "BNE     loc_FF8C0FBC\n"
"loc_FF8C0FAC:\n"
        "LDR     R0, [SP,#0x1C]\n"
        "ADD     R1, SP, #0xC\n"
        "BL      sub_FF8C0E04\n"
        "B       loc_FF8C0F48\n"
"loc_FF8C0FBC:\n"
        "CMP     R1, #0x2B\n"
        "BNE     loc_FF8C0FD4\n"
        "BL      sub_FF8B0A00\n"
        "BL      sub_FF8B1624\n"
        "BL      sub_FF8B1178\n"
        "B       loc_FF8C0F48\n"
"loc_FF8C0FD4:\n"
        "CMP     R1, #0x2C\n"
        "BNE     loc_FF8C1024\n"
        "LDR     R0, [R7,#0x1C]\n"
        "MOV     R1, #4\n"
        "BL      sub_FF884914\n"
        "LDR     R1, =0xFF8BD5CC\n"
        "LDR     R0, =0xFFFFF400\n"
        "MOV     R2, #4\n"
        "BL      sub_FF8B047C\n"
        "BL      sub_FF8B0704\n"
        "LDR     R0, [R7,#0x1C]\n"
        "MOV     R2, R8\n"
        "MOV     R1, #4\n"
        "BL      sub_FF88473C\n"
        "TST     R0, #1\n"
        "BEQ     loc_FF8C0F48\n"
        "LDR     R1, =0xE91\n"
"loc_FF8C1018:\n"
        "LDR     R0, =0xFF8BDC0C\n"
        "BL      sub_FF81E88C\n"
        "B       loc_FF8C0F48\n"
"loc_FF8C1024:\n"
        "LDR     R0, [SP,#0x1C]\n"
        "MOV     R4, #1\n"
        "LDR     R1, [R0]\n"
        "CMP     R1, #0x11\n"
        "CMPNE   R1, #0x12\n"
        "BNE     loc_FF8C1094\n"
        "LDR     R1, [R0,#0x7C]\n"
        "ADD     R1, R1, R1,LSL#1\n"
        "ADD     R1, R0, R1,LSL#2\n"
        "SUB     R1, R1, #8\n"
        "LDMIA   R1, {R2-R4}\n"
        "STMIA   R6, {R2-R4}\n"
        //"BL      loc_FF8BF784\n"
		"BL      sub_FF8BF784\n"
        "LDR     R0, [SP,#0x1C]\n"
        "LDR     R1, [R0,#0x7C]\n"
        "LDR     R3, [R0,#0x8C]\n"
        "LDR     R2, [R0,#0x90]\n"
        "ADD     R0, R0, #4\n"
        "BLX     R3\n"
        "LDR     R0, [SP,#0x1C]\n"
        "BL      sub_FF8C2524\n"
        "LDR     R0, [SP,#0x1C]\n"
        "LDR     R1, [R0,#0x7C]\n"
        "LDR     R3, [R0,#0x94]\n"
        "LDR     R2, [R0,#0x98]\n"
        "ADD     R0, R0, #4\n"
        "BLX     R3\n"
        "B       loc_FF8C138C\n"
"loc_FF8C1094:\n"
        "CMP     R1, #0x13\n"
        "CMPNE   R1, #0x14\n"
        "CMPNE   R1, #0x15\n"
        "CMPNE   R1, #0x16\n"
        "BNE     loc_FF8C114C\n"
        "ADD     R3, SP, #0xC\n"
        "MOV     R2, SP\n"
        "ADD     R1, SP, #0x10\n"
        "BL      sub_FF8BFA60\n"
        "CMP     R0, #1\n"
        "MOV     R4, R0\n"
        "CMPNE   R4, #5\n"
        "BNE     loc_FF8C10E8\n"
        "LDR     R0, [SP,#0x1C]\n"
        "MOV     R2, R4\n"
        "LDR     R1, [R0,#0x7C]!\n"
        "LDR     R12, [R0,#0x10]!\n"
        "LDR     R3, [R0,#4]\n"
        "MOV     R0, SP\n"
        "BLX     R12\n"
        "B       loc_FF8C1120\n"
"loc_FF8C10E8:\n"
        "LDR     R0, [SP,#0x1C]\n"
        "CMP     R4, #2\n"
        "LDR     R3, [R0,#0x90]\n"
        "CMPNE   R4, #6\n"
        "BNE     loc_FF8C1134\n"
        "LDR     R12, [R0,#0x8C]\n"
        "MOV     R0, SP\n"
        "MOV     R2, R4\n"
        "MOV     R1, #1\n"
        "BLX     R12\n"
        "LDR     R0, [SP,#0x1C]\n"
        "MOV     R2, SP\n"
        "ADD     R1, SP, #0x10\n"
        "BL      sub_FF8C0B0C\n"
"loc_FF8C1120:\n"
        "LDR     R0, [SP,#0x1C]\n"
        "LDR     R2, [SP,#0xC]\n"
        "MOV     R1, R4\n"
        "BL      sub_FF8C0DA4\n"
        "B       loc_FF8C138C\n"
"loc_FF8C1134:\n"
        "LDR     R1, [R0,#0x7C]\n"
        "LDR     R12, [R0,#0x8C]\n"
        "ADD     R0, R0, #4\n"
        "MOV     R2, R4\n"
        "BLX     R12\n"
        "B       loc_FF8C138C\n"
"loc_FF8C114C:\n"
        "CMP     R1, #0x22\n"
        "CMPNE   R1, #0x23\n"
        "BNE     loc_FF8C1198\n"
        "LDR     R1, [R0,#0x7C]\n"
        "ADD     R1, R1, R1,LSL#1\n"
        "ADD     R1, R0, R1,LSL#2\n"
        "SUB     R1, R1, #8\n"
        "LDMIA   R1, {R2-R4}\n"
        "STMIA   R6, {R2-R4}\n"
        "BL      sub_FF8BECC8\n"
        "LDR     R0, [SP,#0x1C]\n"
        "LDR     R1, [R0,#0x7C]\n"
        "LDR     R3, [R0,#0x8C]\n"
        "LDR     R2, [R0,#0x90]\n"
        "ADD     R0, R0, #4\n"
        "BLX     R3\n"
        "LDR     R0, [SP,#0x1C]\n"
        "BL      sub_FF8BEFC8\n"
        "B       loc_FF8C138C\n"
"loc_FF8C1198:\n"
        "ADD     R1, R0, #4\n"
        "LDMIA   R1, {R2,R3,R12}\n"
        "STMIA   R6, {R2,R3,R12}\n"
        "LDR     R1, [R0]\n"
        "CMP     R1, #0x25\n"
        "ADDLS   PC, PC, R1,LSL#2\n"
        "B       loc_FF8C136C\n"
"loc_FF8C11B4:\n"
        "B       loc_FF8C124C\n"
"loc_FF8C11B8:\n"
        "B       loc_FF8C124C\n"
"loc_FF8C11BC:\n"
        "B       loc_FF8C1254\n"
"loc_FF8C11C0:\n"
        "B       loc_FF8C125C\n"
"loc_FF8C11C4:\n"
        "B       loc_FF8C125C\n"
"loc_FF8C11C8:\n"
        "B       loc_FF8C125C\n"
"loc_FF8C11CC:\n"
        "B       loc_FF8C124C\n"
"loc_FF8C11D0:\n"
        "B       loc_FF8C1254\n"
"loc_FF8C11D4:\n"
        "B       loc_FF8C125C\n"
"loc_FF8C11D8:\n"
        "B       loc_FF8C125C\n"
"loc_FF8C11DC:\n"
        "B       loc_FF8C1274\n"
"loc_FF8C11E0:\n"
        "B       loc_FF8C1274\n"
"loc_FF8C11E4:\n"
        "B       loc_FF8C1360\n"
"loc_FF8C11E8:\n"
        "B       loc_FF8C1368\n"
"loc_FF8C11EC:\n"
        "B       loc_FF8C1368\n"
"loc_FF8C11F0:\n"
        "B       loc_FF8C1368\n"
"loc_FF8C11F4:\n"
        "B       loc_FF8C1368\n"
"loc_FF8C11F8:\n"
        "B       loc_FF8C136C\n"
"loc_FF8C11FC:\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1200:\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1204:\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1208:\n"
        "B       loc_FF8C136C\n"
"loc_FF8C120C:\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1210:\n"
        "B       loc_FF8C1264\n"
"loc_FF8C1214:\n"
        "B       loc_FF8C126C\n"
"loc_FF8C1218:\n"
        "B       loc_FF8C126C\n"
"loc_FF8C121C:\n"
        "B       loc_FF8C1280\n"
"loc_FF8C1220:\n"
        "B       loc_FF8C1280\n"
"loc_FF8C1224:\n"
        "B       loc_FF8C1288\n"
"loc_FF8C1228:\n"
        "B       loc_FF8C12B8\n"
"loc_FF8C122C:\n"
        "B       loc_FF8C12E8\n"
"loc_FF8C1230:\n"
        "B       loc_FF8C1318\n"
"loc_FF8C1234:\n"
        "B       loc_FF8C1348\n"
"loc_FF8C1238:\n"
        "B       loc_FF8C1348\n"
"loc_FF8C123C:\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1240:\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1244:\n"
        "B       loc_FF8C1350\n"
"loc_FF8C1248:\n"
        "B       loc_FF8C1358\n"
"loc_FF8C124C:\n"
        "BL      sub_FF8BDAB4\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1254:\n"
        "BL      sub_FF8BDD38\n"
        "B       loc_FF8C136C\n"
"loc_FF8C125C:\n"
        "BL      sub_FF8BDF40\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1264:\n"
        "BL      sub_FF8BE1B8\n"
        "B       loc_FF8C136C\n"
"loc_FF8C126C:\n" // ; jumptable FF8C11AC entries 24,25
        "BL      sub_FF8BE3B0\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1274:\n" //; jumptable FF8C11AC entries 10,11
        //"BL      sub_FF8BE66C\n"
		"BL      sub_FF8BE66C_my\n" // ------------------->
        "MOV     R4, #0\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1280:\n" // ; jumptable FF8C11AC entries 26,27
        "BL      sub_FF8BE7AC\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1288:\n"
        "LDRH    R1, [R0,#4]\n"
        "STRH    R1, [SP,#0x10]\n"
        "LDRH    R1, [R5,#2]\n"
        "STRH    R1, [SP,#0x12]\n"
        "LDRH    R1, [R5,#4]\n"
        "STRH    R1, [SP,#0x14]\n"
        "LDRH    R1, [R5,#6]\n"
        "STRH    R1, [SP,#0x16]\n"
        "LDRH    R1, [R0,#0xC]\n"
        "STRH    R1, [SP,#0x18]\n"
        "BL      sub_FF8C21D0\n"
        "B       loc_FF8C136C\n"
"loc_FF8C12B8:\n"
        "LDRH    R1, [R0,#4]\n"
        "STRH    R1, [SP,#0x10]\n"
        "LDRH    R1, [R5,#2]\n"
        "STRH    R1, [SP,#0x12]\n"
        "LDRH    R1, [R5,#4]\n"
        "STRH    R1, [SP,#0x14]\n"
        "LDRH    R1, [R5,#6]\n"
        "STRH    R1, [SP,#0x16]\n"
        "LDRH    R1, [R5,#8]\n"
        "STRH    R1, [SP,#0x18]\n"
        "BL      sub_FF8C233C\n"
        "B       loc_FF8C136C\n"
"loc_FF8C12E8:\n"
        "LDRH    R1, [R5]\n"
        "STRH    R1, [SP,#0x10]\n"
        "LDRH    R1, [R0,#6]\n"
        "STRH    R1, [SP,#0x12]\n"
        "LDRH    R1, [R5,#4]\n"
        "STRH    R1, [SP,#0x14]\n"
        "LDRH    R1, [R5,#6]\n"
        "STRH    R1, [SP,#0x16]\n"
        "LDRH    R1, [R5,#8]\n"
        "STRH    R1, [SP,#0x18]\n"
        "BL      sub_FF8C23E8\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1318:\n"
        "LDRH    R1, [R5]\n"
        "STRH    R1, [SP,#0x10]\n"
        "LDRH    R1, [R5,#2]\n"
        "STRH    R1, [SP,#0x12]\n"
        "LDRH    R1, [R5,#4]\n"
        "STRH    R1, [SP,#0x14]\n"
        "LDRH    R1, [R5,#6]\n"
        "STRH    R1, [SP,#0x16]\n"
        "LDRH    R1, [R0,#0xC]\n"
        "STRH    R1, [SP,#0x18]\n"
        "BL      sub_FF8C2488\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1348:\n"
        "BL      sub_FF8BEB04\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1350:\n"
        "BL      sub_FF8BF0CC\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1358:\n"
        "BL      sub_FF8BF308\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1360:\n"
        "BL      sub_FF8BF484\n"
        "B       loc_FF8C136C\n"
"loc_FF8C1368:\n"
        "BL      sub_FF8BF620\n"
"loc_FF8C136C:\n"
        "LDR     R0, [SP,#0x1C]\n"
        "LDR     R1, [R0,#0x7C]\n"
        "LDR     R3, [R0,#0x8C]\n"
        "LDR     R2, [R0,#0x90]\n"
        "ADD     R0, R0, #4\n"
        "BLX     R3\n"
        "CMP     R4, #1\n"
        "BNE     loc_FF8C13D4\n"
"loc_FF8C138C:\n"
        "LDR     R0, [SP,#0x1C]\n"
        "MOV     R2, #0xC\n"
        "LDR     R1, [R0,#0x7C]\n"
        "ADD     R1, R1, R1,LSL#1\n"
        "ADD     R0, R0, R1,LSL#2\n"
        "SUB     R4, R0, #8\n"
        "LDR     R0, =0x4372C\n"
        "ADD     R1, SP, #0x10\n"
        "BL      sub_FFB34BC8\n"
        "LDR     R0, =0x43738\n"
        "MOV     R2, #0xC\n"
        "ADD     R1, SP, #0x10\n"
        "BL      sub_FFB34BC8\n"
        "LDR     R0, =0x43744\n"
        "MOV     R2, #0xC\n"
        "MOV     R1, R4\n"
        "BL      sub_FFB34BC8\n"
        "B       loc_FF8C144C\n"
"loc_FF8C13D4:\n"
        "LDR     R0, [SP,#0x1C]\n"
        "LDR     R0, [R0]\n"
        "CMP     R0, #0xB\n"
        "BNE     loc_FF8C141C\n"
        "MOV     R3, #0\n"
        "STR     R3, [SP]\n"
        "MOV     R3, #1\n"
        "MOV     R2, #1\n"
        "MOV     R1, #1\n"
        "MOV     R0, #0\n"
        "BL      sub_FF8BD8BC\n"
        "MOV     R3, #0\n"
        "STR     R3, [SP]\n"
        "MOV     R3, #1\n"
        "MOV     R2, #1\n"
        "MOV     R1, #1\n"
        "MOV     R0, #0\n"
        "B       loc_FF8C1448\n"
"loc_FF8C141C:\n"
        "MOV     R3, #1\n"
        "MOV     R2, #1\n"
        "MOV     R1, #1\n"
        "MOV     R0, #1\n"
        "STR     R3, [SP]\n"
        "BL      sub_FF8BD8BC\n"
        "MOV     R3, #1\n"
        "MOV     R2, #1\n"
        "MOV     R1, #1\n"
        "MOV     R0, #1\n"
        "STR     R3, [SP]\n"
"loc_FF8C1448:\n"
        "BL      sub_FF8BD9FC\n"
"loc_FF8C144C:\n"
        "LDR     R0, [SP,#0x1C]\n"
        "BL      sub_FF8C215C\n"
        "B       loc_FF8C0E74\n"
);
};

void __attribute__((naked,noinline)) sub_FF8BE66C_my(){
 asm volatile(
"sub_FF8BE66C:\n"
        "STMFD   SP!, {R4-R8,LR}\n"
        "LDR     R7, =0x4440\n"
        "MOV     R4, R0\n"
        "LDR     R0, [R7,#0x1C]\n"
        "MOV     R1, #0x3E\n"
        "BL      sub_FF884914\n"
        "LDRSH   R0, [R4,#4]\n"
        "MOV     R2, #0\n"
        "MOV     R1, #0\n"
        "BL      sub_FF8BD620\n"
        "MOV     R6, R0\n"
        "LDRSH   R0, [R4,#6]\n"
        "BL      sub_FF8BD730\n"
        "LDRSH   R0, [R4,#8]\n"
        "BL      sub_FF8BD788\n"
        "LDRSH   R0, [R4,#0xA]\n"
        "BL      sub_FF8BD7E0\n"
        "LDRSH   R0, [R4,#0xC]\n"
        "MOV     R1, #0\n"
        "BL      sub_FF8BD838\n"
        "MOV     R5, R0\n"
        "LDR     R0, [R4]\n"
        "LDR     R8, =0x43744\n"
        "CMP     R0, #0xB\n"
        "MOVEQ   R6, #0\n"
        "MOVEQ   R5, #0\n"
        "BEQ     loc_FF8BE700\n"
        "CMP     R6, #1\n"
        "BNE     loc_FF8BE700\n"
        "LDRSH   R0, [R4,#4]\n"
        "LDR     R1, =0xFF8BD58C\n"
        "MOV     R2, #2\n"
        "BL      sub_FF959870\n"
        "STRH    R0, [R4,#4]\n"
        "MOV     R0, #0\n"
        "STR     R0, [R7,#0x28]\n"
        "B       loc_FF8BE708\n"
"loc_FF8BE700:\n"
        "LDRH    R0, [R8]\n"
        "STRH    R0, [R4,#4]\n"
"loc_FF8BE708:\n"
        "CMP     R5, #1\n"
        "LDRNEH  R0, [R8,#8]\n"
        "BNE     loc_FF8BE724\n"
        "LDRSH   R0, [R4,#0xC]\n"
        "LDR     R1, =0xFF8BD610\n"
        "MOV     R2, #0x20\n"
        "BL      sub_FF8C218C\n"
"loc_FF8BE724:\n"
        "STRH    R0, [R4,#0xC]\n"
        "LDRSH   R0, [R4,#6]\n"
        //"BL      sub_FF8B0770\n"
		"BL      sub_FF8B0770_my\n" // -------------------->

        "LDRSH   R0, [R4,#8]\n"
        "MOV     R1, #1\n"
        "BL      sub_FF8B0EC0\n"
        "MOV     R1, #0\n"
        "ADD     R0, R4, #8\n"
        "BL      sub_FF8B0F48\n"
        "LDRSH   R0, [R4,#0xE]\n"
        "BL      sub_FF8B8A2C\n"
        "LDR     R4, =0xBB8\n"
        "CMP     R6, #1\n"
        "BNE     loc_FF8BE77C\n"
        "LDR     R0, [R7,#0x1C]\n"
        "MOV     R2, R4\n"
        "MOV     R1, #2\n"
        "BL      sub_FF884820\n"
        "TST     R0, #1\n"
        "LDRNE   R1, =0x5A3\n"
        "LDRNE   R0, =0xFF8BDC0C\n"
        "BLNE    sub_FF81E88C\n"
"loc_FF8BE77C:\n"
        "CMP     R5, #1\n"
        "LDMNEFD SP!, {R4-R8,PC}\n"
        "LDR     R0, [R7,#0x1C]\n"
        "MOV     R2, R4\n"
        "MOV     R1, #0x20\n"
        "BL      sub_FF884820\n"
        "TST     R0, #1\n"
        "LDRNE   R1, =0x5A8\n"
        "LDRNE   R0, =0xFF8BDC0C\n"
        "LDMNEFD SP!, {R4-R8,LR}\n"
        "BNE     sub_FF81E88C\n"
        "LDMFD   SP!, {R4-R8,PC}\n"
 );
};
void __attribute__((naked,noinline)) sub_FF8B0770_my()
{  
 asm volatile(
        "STMFD   SP!, {R4-R6,LR}\n"
        "LDR     R5, =0x4150\n"
        "MOV     R4, R0\n"
        "LDR     R0, [R5,#4]\n"
        "CMP     R0, #1\n"
        "LDRNE   R1, =0x146\n"
        "LDRNE   R0, =0xFF8B0574\n"
        "BLNE    sub_FF81E88C\n"
        "CMN     R4, #0xC00\n"
        "LDREQSH R4, [R5,#2]\n"
        "CMN     R4, #0xC00\n"
        "MOVEQ   R1, #0x14C\n"
        //"ADREQ   R0, 0xFF8B0574\n"
		"LDREQ   R0, =0xFF8B0574\n"
        "STRH    R4, [R5,#2]\n"
        "BLEQ    sub_FF81E88C\n"
        "MOV     R0, R4\n"

        //"BL      sub_FFA0D7A8\n" // -
		"BL      apex2us\n"  // +

        "MOV     R4, R0\n"
        "BL      sub_FF8F5240\n"
        "MOV     R0, R4\n"
        "BL      sub_FF8FE10C\n"
        "TST     R0, #1\n"
        "LDRNE   R1, =0x151\n"
        "LDMNEFD SP!, {R4-R6,LR}\n"
        "LDRNE   R0, =0xFF8B0574\n"
        "BNE     sub_FF81E88C\n"
        "LDMFD   SP!, {R4-R6,PC}\n"
     );
};
